<?php
/*
 * 发送email
 *
 */
class LibsMail {

    public static $debug = 0;
    private static $_import = false;
    private static $_instance = null;

    private static function _init()
    {
        if (!empty(self::$_instance)) {
            return self::$_instance;
        }

        self::$_import = self::$_import ? true : LibsCommon::importThirdLibrary('phpmail');

        $emailConf = LibsCommon::getConfig('email');

        $mail = new PHPMailer(); 
        $mail->IsSMTP();
        self::$debug && $mail->SMTPDebug = self::$debug;
        $mail->SMTPAuth = true;
        $mail->SMTPSecure = "ssl";             
        $mail->Host = $emailConf['host'];
        $mail->Port = $emailConf['port'];
        $mail->Username = $emailConf['user'];
        $mail->Password = $emailConf['pass'];
        
        $mail->SetFrom($emailConf['user'], $emailConf['user']);
        $mail->AddReplyTo($emailConf['user'], $emailConf['user']);

        return self::$_instance = $mail;
    }

    /**
     *  自定义发送
     *
     * @param array $param param 
     * array('files'=>array('fileName'=>'filePath'), 'subject'=>'', 'html'=>'')
     * @param array $emails emails
     *
     * @return bool
     */
    public static function send($params = array(), $emails = array())
    {
        $mail = self::_init();        
        //发送文件
        if (!empty($params['files'])) {
            foreach ($params['files'] as $k => $v) {
                $mail->AddAttachment($v, $k);
            }
        }

        foreach ($emails as $v) {
            $mail->AddAddress($v);
        }

        $mail->Subject = empty($params['subject']) ? ' ': $params['subject'];

        $html = empty($params['html']) ? ' ': $params['html'];
        $mail->MsgHTML($html);

        return $mail->Send();
    }
}


